package com.sgcc.ln.pmos.common.utils.picture;

import android.app.Activity;
import android.graphics.Color;

import androidx.fragment.app.Fragment;

import com.luck.picture.lib.basic.PictureSelector;
import com.luck.picture.lib.config.SelectMimeType;
import com.luck.picture.lib.config.SelectModeConfig;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.style.BottomNavBarStyle;
import com.luck.picture.lib.style.PictureSelectorStyle;
import com.luck.picture.lib.style.SelectMainStyle;
import com.luck.picture.lib.style.TitleBarStyle;
import com.sgcc.pmos.nativesdk.R;

import java.util.List;

public class PictureSelectUtil {

    // LocalMedia 里面返回三种path
    // 1.media.getPath(); 为原图path
    // 2.media.getCutPath();为裁剪后path，需判断media.isCut();是否为true
    // 3.media.getCompressPath();为压缩后path，需判断media.isCompressed();是否为true
    // 如果裁剪并压缩了，已取压缩路径为准，因为是先裁剪后压缩的
    public static String getPath(LocalMedia localMedia) {
        if (localMedia == null) {
            return null;
        }

        if (localMedia.isCompressed()) {
            return localMedia.getCompressPath();
        } else if (localMedia.isCut()) {
            return localMedia.getCutPath();
        } else {
            return localMedia.getPath();
        }
    }

    public static void select(Fragment fragment, int requestCode, List<LocalMedia> selectList) {
        select(null, fragment, requestCode, false, false, selectList);
    }

    public static void select(Fragment fragment, int requestCode,boolean isSingle,  List<LocalMedia> selectList) {
        select(null, fragment, requestCode, isSingle, false, selectList);
    }

    public static void selectAvatar(Fragment fragment, int requestCode, List<LocalMedia>
            selectList) {
        select(null, fragment, requestCode, true, true, selectList);
    }

    public static void select(Activity activity, int requestCode, List<LocalMedia> selectList) {
        select(activity, null, requestCode, false, false, selectList);
    }

    public static void select(Activity activity, boolean isSingle, int requestCode, List<LocalMedia> selectList) {
        select(activity, null, requestCode, isSingle, false, selectList);
    }

    public static void selectAvatar(Activity activity, int requestCode, List<LocalMedia> selectList) {
        select(activity, null, requestCode, true, true, selectList);
    }

    public static void select(Activity activity, Fragment fragment, int requestCode, boolean
            isSingle, boolean isCrop, List<LocalMedia> selectList) {
        PictureSelector pictureSelector;
        if (activity != null) {
            pictureSelector = PictureSelector.create(activity);
        } else {
            pictureSelector = PictureSelector.create(fragment);
        }
        pictureSelector
                .openGallery(SelectMimeType.ofImage())//全部.PictureMimeType.ofAll()、图片.ofImage()、视频.ofVideo()、音频.ofAudio()
                .setImageEngine(GlideEngine.createGlideEngine())// 设置相册图片加载引擎
                .setCompressEngine(new ImageFileCompressEngine())// 设置相册压缩引擎
                .setSelectorUIStyle(getCustomUIStyle())//主题样式
                .setMaxSelectNum(9)// 最大图片选择数量
                .setMinSelectNum(1)// 最小选择数量
                .setImageSpanCount(4)// 每行显示个数
                .isMaxSelectEnabledMask(true)//达到最大选择数是否开启禁选蒙层
                .setSelectionMode(isSingle ? SelectModeConfig.SINGLE : SelectModeConfig.MULTIPLE)// 多选 or 单选
                .isDirectReturnSingle(isSingle)// 单选时是否立即返回
                .isPreviewImage(true)// 是否支持预览图片
                .isPreviewVideo(true)// 是否支持预览视频
                .isPreviewAudio(true)// 是否可播放音频
                .isDisplayCamera(true)// 是否显示相机入口
                .isOpenClickSound(false)// 是否开启点击音效
                .isLoopAutoVideoPlay(false)// 预览视频是否循环播放
                .isSelectZoomAnim(true)// 选择缩略图缩放效果
                .setSelectedData(selectList)// 是否传入已选图片 List<LocalMedia> list
                .forResult(requestCode);//结果回调onActivityResult code
    }

    public static void selectImageAndVideo(Activity activity, Fragment fragment, int requestCode, boolean
            isSingle, List<LocalMedia> selectList) {
        PictureSelector pictureSelector;
        if (activity != null) {
            pictureSelector = PictureSelector.create(activity);
        } else {
            pictureSelector = PictureSelector.create(fragment);
        }
        pictureSelector
                .openGallery(SelectMimeType.ofAll())//全部.PictureMimeType.ofAll()、图片.ofImage()、视频.ofVideo()、音频.ofAudio()
                .setImageEngine(GlideEngine.createGlideEngine())// 设置相册图片加载引擎
                .setCompressEngine(new ImageFileCompressEngine())// 设置相册压缩引擎
                .setSelectorUIStyle(getCustomUIStyle())//主题样式
                .setMaxSelectNum(9)// 最大图片选择数量
//                .setMinSelectNum(1)// 最小选择数量
                .setImageSpanCount(4)// 每行显示个数
                .setMaxVideoSelectNum(1)//视频最大选择数
                .isMaxSelectEnabledMask(false)//达到最大选择数是否开启禁选蒙层
                .setSelectionMode(isSingle ? SelectModeConfig.SINGLE : SelectModeConfig.MULTIPLE)// 多选 or 单选
                .isDirectReturnSingle(isSingle)// 单选时是否立即返回
                .isPreviewImage(true)// 是否支持预览图片
                .isPreviewVideo(true)// 是否支持预览视频
                .isWithSelectVideoImage(true)//是否支持视频和图片同选
                .isPreviewAudio(true)// 是否可播放音频
                .isDisplayCamera(true)// 是否显示相机入口
                .isOpenClickSound(false)// 是否开启点击音效
                .isLoopAutoVideoPlay(false)// 预览视频是否循环播放
                .isSelectZoomAnim(true)// 选择缩略图缩放效果
                .setSelectedData(selectList)// 是否传入已选图片 List<LocalMedia> list
                .forResult(requestCode);//结果回调onActivityResult code
    }

    private static PictureSelectorStyle getCustomUIStyle() {
        PictureSelectorStyle selectorStyle = new PictureSelectorStyle();

        TitleBarStyle whiteTitleBarStyle = new TitleBarStyle();
        whiteTitleBarStyle.setTitleBackgroundColor(Color.parseColor("#FFFFFF"));
        whiteTitleBarStyle.setTitleDrawableRightResource(R.mipmap.ic_orange_arrow_down);
        whiteTitleBarStyle.setTitleLeftBackResource(R.mipmap.icon_back);
        whiteTitleBarStyle.setTitleTextColor(Color.parseColor("#000000"));
        whiteTitleBarStyle.setTitleCancelTextColor(Color.parseColor("#000000"));
        whiteTitleBarStyle.setDisplayTitleBarLine(false);

        BottomNavBarStyle numberBlueBottomNavBarStyle = new BottomNavBarStyle();
        numberBlueBottomNavBarStyle.setBottomPreviewNormalTextColor(Color.parseColor("#999999"));
        numberBlueBottomNavBarStyle.setBottomPreviewSelectTextColor(Color.parseColor("#FF662D"));
        numberBlueBottomNavBarStyle.setBottomNarBarBackgroundColor(Color.parseColor("#F6F6F6"));
        numberBlueBottomNavBarStyle.setBottomSelectNumResources(R.drawable.module_base_bg_picture_num_selected);
        numberBlueBottomNavBarStyle.setBottomEditorTextColor(Color.parseColor("#53575E"));
        numberBlueBottomNavBarStyle.setBottomOriginalTextColor(Color.parseColor("#53575E"));

        SelectMainStyle numberBlueSelectMainStyle = new SelectMainStyle();
        numberBlueSelectMainStyle.setStatusBarColor(Color.parseColor("#FF662D"));
        numberBlueSelectMainStyle.setSelectNumberStyle(true);
        numberBlueSelectMainStyle.setDarkStatusBarBlack(true);
        numberBlueSelectMainStyle.setPreviewSelectNumberStyle(true);
        numberBlueSelectMainStyle.setSelectBackground(R.drawable.module_base_bg_picture_num_selector);
        numberBlueSelectMainStyle.setMainListBackgroundColor(Color.parseColor("#FFFFFF"));
        numberBlueSelectMainStyle.setPreviewSelectBackground(R.drawable.module_base_picture_preview_num_selector);
        numberBlueSelectMainStyle.setSelectNormalTextColor(Color.parseColor("#999999"));
        numberBlueSelectMainStyle.setSelectTextColor(Color.parseColor("#FF662D"));
        numberBlueSelectMainStyle.setSelectText("已完成");

        selectorStyle.setTitleBarStyle(whiteTitleBarStyle);
        selectorStyle.setBottomBarStyle(numberBlueBottomNavBarStyle);
        selectorStyle.setSelectMainStyle(numberBlueSelectMainStyle);
        return selectorStyle;
    }

}
